﻿using MediatR;
using W.Warehouse.Api.Application.Command.WarehouseCommand;
using W.Warehouse.Api.Application.Command.WarehouseTypeCommand;
using W.Warehouse.Api.Application.CommandHandlder.WarehouseTypeCommandHandlder;
using W.Warehouse.Domain;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Api.Application.CommandHandlder.WarehouseCommandHandlder
{
    public class WarehouseDelectCommandHandlder : IRequestHandler<WarehouseDelectCommand, ApiResult<int>>
    {
        private readonly ILogger<WarehouseDelectCommandHandlder> _logger;
        private readonly IWarehouseInfoRepository _warehouseInfoRepository;
        public WarehouseDelectCommandHandlder(IWarehouseInfoRepository warehouseInfoRepository, ILogger<WarehouseDelectCommandHandlder> logger)
        {
            _warehouseInfoRepository = warehouseInfoRepository;
            _logger = logger;
        }
        public async Task<ApiResult<int>> Handle(WarehouseDelectCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                _logger.LogInformation("删除仓库管理成功");
                var res = await _warehouseInfoRepository.GetModelAsync(request.warehouseId);
                res.IsDelete = true;
                res.DeleteTime = DateTime.Now;
                //res.DeleteName = "张三";
                result.Code = await _warehouseInfoRepository.UpdateAsync(res);
            }
            catch (Exception el)
            {
                result.Code = -1;
                result.Message = el.Message;
                _logger.LogError(el, el.Message);
                throw;
            }
            return result;
        }
    }
}
